﻿using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils.Text;
using DevExpress.XtraEditors;
using EduFormManager.Forms.UserControls;
using EduFormManager.Properties;

namespace EduFormManager.Forms
{
    public partial class CollapsableSideListBoxControl : XtraBaseControl
    {

        private Color m_backColor = Color.FromArgb(2, 83, 142);
        private Color m_foreColor = Color.White;
        private int m_hoveredIndex;
        private Image m_image = new Bitmap(Resources.close01_white);
        private Rectangle m_lastRectangle = new Rectangle(0,0,16,16);
        public CollapsableSideListBoxControl()
        {
            InitializeComponent();

            this.listBoxControl1.DataSource = Notifications.List;
        }
        
        public void lst_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            ListBoxControl lst = sender as ListBoxControl;
            if (e.Index >= 0)
                e.ItemHeight = (int)e.Graphics.MeasureString(lst.GetItemText(e.Index), lst.Font, lst.Width).Height + 5;
        }

        private void listBoxControl1_DrawItem(object sender, ListBoxDrawItemEventArgs e)
        {
            ListBoxControl lst = sender as ListBoxControl;

            e.Cache.FillRectangle(new SolidBrush(m_backColor), e.Bounds);
            if (e.Index == m_hoveredIndex)
            {
                m_foreColor = Color.FromArgb(0xD8, 0xD8, 0xD8);
                Rectangle imgRect = new Rectangle(0, 0, 16, 16);
                imgRect.Location = new Point(e.Bounds.Right - imgRect.Width, e.Bounds.Top + (e.Bounds.Height - imgRect.Height) / 2);
                e.Graphics.DrawImage(m_image, imgRect);
            }
            else
            {
                if (e.Item is Notifications.ExpireWarn)
                {
                    Notifications.ExpireWarn warn = e.Item as Notifications.ExpireWarn;
                    if (warn.DaysRemain < 4)
                        m_foreColor = Color.LightCoral;
                    else if (warn.DaysRemain >= 4 && warn.DaysRemain < 7)
                        m_foreColor = Color.Khaki;
                    else
                        m_foreColor = Color.PaleGreen;
                }
                else
                {
                    m_foreColor = Color.White;
                }
            }

            TextUtils.DrawString(e.Graphics, lst.GetItemText(e.Index), lst.Appearance.Font, m_foreColor, e.Bounds);


            e.Handled = true;
        }

        
        private void listBoxControl1_MouseMove(object sender, MouseEventArgs e)
        {
            ListBoxControl lst = sender as ListBoxControl;
            int idx = lst.IndexFromPoint(e.Location);
            if (idx != m_hoveredIndex)
            {
                m_hoveredIndex = idx;
                lst.Invalidate();
            }
            Rectangle itemRect = lst.GetItemRectangle(idx);
            Rectangle imgRect = new Rectangle(0, 0, 16, 16);
            imgRect.Location = new Point(itemRect.Right - imgRect.Width, itemRect.Top + (itemRect.Height - imgRect.Height) / 2);
            if (imgRect.Contains(e.Location))
            {
                this.Cursor = Cursors.Hand;
                m_lastRectangle = imgRect;
            }
            else
            {
                this.Cursor = Cursors.Arrow;
            }
        }

        private void listBoxControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (m_lastRectangle.Contains(e.Location))
                {
                    ListBoxControl lst = sender as ListBoxControl;
                    int idx = lst.IndexFromPoint(e.Location);
                    object val = lst.GetItem(idx);
                    Notifications.Remove(val);
                }
            }
        }
    }
}
